#!/bin/bash
#
# Copyright (c) 2013, Duke Fong <duke@dukelec.com>
#
# All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation version 2 of
# the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#

read_dom () {
    local IFS=\>
    read -d \< ENTITY CONTENT
    local RET=$?
    TAG_NAME=${ENTITY%% *}
    ATTRIBUTES=${ENTITY#* }
    return $RET
}

parse_project_dom () {
    if [[ $TAG_NAME = "project" ]] ; then
        eval local ${ATTRIBUTES%/}
        echo $name
    fi
}

parse_non_default_remote () {
    if [[ $TAG_NAME = "project" ]] ; then
        eval local ${ATTRIBUTES%/}

        #echo "$path --> $name --> $groups"

        if [ x"" != x"$remote" ]; then
              echo "  <remove-project name=\"$name\" />" >> ~/.repo-annex/local_manifests/eliminated.xml
        fi
    fi
}

parse_non_exist () {
    if [[ $TAG_NAME = "project" ]] ; then
        eval local ${ATTRIBUTES%/}

        for i in $EXISTED_LIST; do
            if [ x$i == x$name ]; then
               return
            fi
        done
        echo "  <project name=\"$name\" />" >> ~/.repo-annex/temp.xml
    fi
}

format_xml () {
    xmllint --format "$1" | sed '/<!--.*-->/d' | sed '/<!--/,/-->/d'
}

#

function repo_exist_test () {
    if [ ! -d .repo ]; then
        echo -e "\nCan't find .repo in current dir!"
        exit
    fi
}

if [ x"$1" == x"--help" ] || [ x"$1" == x"" ] ; then
    echo -e "Usage:\n"
    echo -e "generate-local_manifests.sh branch1 branch2:non_default.xml branch3\n"
    echo -e "this tool is used for multi-branch repo mirror\n"
    echo "ver 0.1"
    exit
fi

repo_exist_test

rm -rf ~/.repo-annex
mkdir ~/.repo-annex

cp -rL .repo/manifests ~/.repo-annex/
echo

mkdir ~/.repo-annex/xmls
# mkdir ~/.repo-annex/local_manifests
cp -r .repo/local_manifests ~/.repo-annex/local_manifests

TOP_DIR="`pwd`"

format_xml .repo/manifest.xml > ~/.repo-annex/manifest.xml

for var in "$@"
do
    # echo "var: $var"
    branch_name=${var%:*}
    if [ x"$branch_name" == x"$var" ]; then
        file_name="default.xml"
    else
        file_name=${var#*:}
    fi
    echo "$branch_name : $file_name"
    cd ~/.repo-annex/manifests
    git clean -dxf
    git reset --hard origin/$branch_name
    
    format_xml "$file_name" > ~/.repo-annex/xmls/"${branch_name}.${file_name}"
    cd -
done

echo
read -n1 -r -p "Press any key to generate remove-project.xml "

echo "<manifest>" > ~/.repo-annex/local_manifests/eliminated.xml
cat ~/.repo-annex/manifest.xml | while read_dom; do parse_non_default_remote "$@"; done
echo "</manifest>" >> ~/.repo-annex/local_manifests/eliminated.xml


read -n1 -r -p "Press any key to generate other xml "

XMLS_LIST=`cd ~/.repo-annex/xmls/; ls *.xml`


for file in $XMLS_LIST
do
    EXISTED_LIST=`cat ~/.repo-annex/manifest.xml ~/.repo-annex/local_manifests/*.xml | while read_dom; do parse_project_dom; done`
    echo "  parsing $file"
    echo "<manifest>" > ~/.repo-annex/temp.xml
    cat ~/.repo-annex/xmls/"$file" | while read_dom; do parse_non_exist "$@"; done
    echo "</manifest>" >> ~/.repo-annex/temp.xml
    mv ~/.repo-annex/temp.xml ~/.repo-annex/local_manifests/"$file"
done

echo
echo "remove empty xml"

for file in ~/.repo-annex/local_manifests/*.xml
do
    temp="$(cat $file | grep project)"
    if [ x"$temp" == x"" ]; then
        rm $file
        echo "  rm $file"
    fi
done

echo
read -n1 -r -p "Press any key to copy file from ~/.repo-annex/local_manifests/ "

mkdir -p .repo/local_manifests
cp ~/.repo-annex/local_manifests/*.xml .repo/local_manifests/

echo
echo "done."
rm -rf ~/.repo-annex
